//
//  YFMyDongtaiCell.m
//  project
//
//  Created by yefei on 2019/5/6.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMyDongtaiCell.h"
#import "YFMineCellViewModel.h"
#import "YBImageBrowser.h"

@interface YFMyDongtaiCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *shadowView;
@property (weak, nonatomic) IBOutlet UIView *bodyView;
@property (weak, nonatomic) IBOutlet UIView *contentsView;
@property (weak, nonatomic) IBOutlet UIButton *pariseButton;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelContraintheight;

/**
 图片浏览数组数组
 */
@property (nonatomic, strong) NSArray <YBImageBrowseCellData *> *imageCellDatas;

/**
 视频浏览数据数组
 */
@property (nonatomic, strong) NSArray <YBVideoBrowseCellData *> *videoCellDatas;


@end

@implementation YFMyDongtaiCell

- (void)setViewModel:(YFMineCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    for (UIView *view in self.contentsView.subviews) {
        [view removeFromSuperview];
    }
    
    [self.pariseButton setTitle:viewModel.praiseAmount forState:(UIControlStateNormal)];
    UIImage *pariseImage = viewModel.isPraise ? [UIImage imageNamed:@"icon_myDongtai_parise_sel"] : [UIImage imageNamed:@"icon_myDongtai_parise_nor"];
    [self.pariseButton setImage:pariseImage forState:(UIControlStateNormal)];
    
    self.titleLabel.attributedText = viewModel.attributeTitle;
    [self.contentsView addSubview:viewModel.content];
    
    self.titleLabelContraintheight.constant = viewModel.titleHeight;
    [self layoutIfNeeded];

//    // 添加点击事件
//    if (viewModel.isImage) {
//
//        // 用于图片浏览的数据源数组
//        NSMutableArray <YBImageBrowseCellData *> *imageCellDatas = [NSMutableArray arrayWithCapacity:9];
//
//        for (UIView *v in viewModel.content.subviews) {
//            UIImageView *iv;
//            if ([v isKindOfClass:UIImageView.class]) {
//                iv = (UIImageView *)v;
//            }
//
//            YBImageBrowseCellData *data = [YBImageBrowseCellData new];
//
//            __block UIImage *image = iv.image;
//            data.imageBlock = ^__kindof UIImage * _Nullable{
//                return image;
//            };
//            data.sourceObject = iv;
//            [imageCellDatas addObject:data];
//
//            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//            [iv addGestureRecognizer:tap];
//
//            @weakify(self)
//            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//                @strongify(self)
//                [self showBrowserImages:x];
//            }];
//        }
//        self.imageCellDatas = imageCellDatas;
//    }
//    else {
//        NSMutableArray <YBVideoBrowseCellData *> * videoCelldatas = [NSMutableArray arrayWithCapacity:1];
//        for (UIView *v in viewModel.content.subviews) {
//            UIImageView *iv;
//            if ([v isKindOfClass:UIImageView.class]) {
//                iv = (UIImageView *)v;
//            }
//
//            YBVideoBrowseCellData *data = [YBVideoBrowseCellData new];
//            data.url = [NSURL URLWithString:viewModel.videoURL];
//            data.sourceObject = iv;
//            data.autoPlayCount = 1;
//            [videoCelldatas addObject:data];
//            self.videoCellDatas = videoCelldatas;
//
//            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//            [iv addGestureRecognizer:tap];
//            @weakify(self)
//            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//                @strongify(self)
//                [self showBrowserVideo];
//            }];
//
//            for (UIView *sv in v.subviews) {
//                UIButton *b;
//                if ([sv isKindOfClass:UIButton.class]) {
//                    b = (UIButton *)sv;
//                }
//
//                [[b rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
//                    @strongify(self)
//                    [self showBrowserVideo];
//                }];
//            }
//        }
//        self.videoCellDatas = videoCelldatas;
//    }
}

- (void)showBrowserVideo {
    YBImageBrowser *browser = [YBImageBrowser new];
    [browser.defaultToolBar hideOperationButton];
    NSInteger currentIndex = 0;
    browser.dataSourceArray = _videoCellDatas;
    [browser.defaultSheetView setHidden:YES];
    browser.currentIndex = currentIndex;
    [browser show];
}

- (void)showBrowserImages:(UIGestureRecognizer *)tap {
    YBImageBrowser *browser = [YBImageBrowser new];
    NSInteger currentIndex = tap.view.tag;
    browser.dataSourceArray = _imageCellDatas;
    [browser.defaultSheetView setHidden:YES];
    [browser.defaultToolBar hideOperationButton];
    browser.currentIndex = currentIndex;
    [browser show];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
}

- (void)prepareForReuse {
    [super prepareForReuse];
   
    for (UIView *view in self.contentsView.subviews) {
        [view removeFromSuperview];
    }
    
     [self setupViews];
}

- (void)setupViews {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.bodyView rounded:(UIRectCornerAllCorners) radius:5];
        self.shadowView.layer.cornerRadius = 5;
        self.shadowView.layer.shadowColor = UIColor.blackColor.CGColor;
        self.shadowView.layer.shadowOffset = CGSizeMake(0, 0);
        self.shadowView.layer.shadowRadius = 2;
        self.shadowView.layer.shadowOpacity = 0.15;
        
        UIBezierPath  *path = [UIBezierPath bezierPathWithRect:self.shadowView.bounds];
        self.shadowView.layer.shadowPath = path.CGPath;
    });
    
    @weakify(self)
    [[self.moreButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.deleteAction) {
            self.deleteAction();
        }
    }];
    
    [[self.pariseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if ([self.title isEqualToString:@"我点的赞"]) {
            self.pariseButton.userInteractionEnabled = NO;
            int zannum = self.pariseButton.titleLabel.text.intValue;
            x.selected = !self.pariseButton.selected;
            if (x.selected) {
                zannum++;
            }else {
                zannum--;
            }
            NSString * url = [Api stringByAppendingString:PraiseApi];
            NSMutableDictionary * params = [NSMutableDictionary dictionary];
            params[@"userId"] = [HJUserManager getUserModel].userId;
            params[@"albumId"] = self->_viewModel.albumId;
            WS(weakSelf)
            [HJHttpTool post:url params:params success:^(HJResponeds *result) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.pariseButton.selected = x.selected;
                    [self.pariseButton setTitle:[NSString stringWithFormat:@"%d",zannum] forState:0];
                    if (self.deleteAction) {
                        self.deleteAction();
                    }
                });
            } failure:^(id result) {
                [YFHud showError:result];
            }];
        }
    }];
}



@end
